package cn.aloxc.stock.context.data
/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 2009-9-24
 * Time: 17:21:17
 * To change this template use File | Settings | File Templates.
 */
import cn.aloxc.stock.fund.*;
import java.util.ArrayList;
import java.util.Date;
import groovy.sql.*;
import cn.aloxc.stock.context.SysContext;
public class GetDataFromHexun
{
		def sql;
/*
资金净流入排名
资金净流入占成交比例排名
资金净流出排名
资金净流出占成交比例排名
机构增仓个股排名
机构减仓个股排名
散户增仓个股排名
散户减仓个股排名
机构与散户博弈个股排名
连续机构增持个股
连续机构减仓个股
 * */
		public static void main(def args){

			def instance = new GetDataFromHexun();
			instance.initConn();
            println instance.checkDate("fundstat");
            println instance.checkDate("boyistat");
            println instance.checkDate("zcggstat");
			 /*
			 def rlta = instance.parseHTML_a();
			System.err.println("正在插入数据..............")
			rlta.each{p ->
              //println p
				instance.insert_a(p);
			}
			def rltb = instance.parseHTML_b();
			System.err.println("正在插入数据..............")

			rltb.each{p ->
			 instance.insert_b(p);
			}

			def rltc = instance.parseHTML_c();
			System.err.println("正在插入数据..............")
			rltc.each{p ->
			  instance.insert_c(p);
			}

			println "处理完毕"
			return;
			*/
		}
        def checkDate(def table)
        {
          def sdf = new java.text.SimpleDateFormat("yyyy-MM-dd");
          def date = new Date();
          def calendar = Calendar.getInstance();
          calendar.setTime(date);
          def cd = date;
          def dayofweek = calendar.get(Calendar.DAY_OF_WEEK);
          if(dayofweek == 1)  //ri
            cd -= 2;
          if(dayofweek == 7)  //liu
            cd -= 1;         
          def cdsdf = sdf.format(cd);
          if(SysContext.STOP_2009.indexOf(cdsdf.toString())==-1 && cd.getHours()>= 16)
          {
              def count = 0 ;
              sql.eachRow("select count(*) from ${table} where statdate = ?",[cdsdf]){
                count = it[0];
              }
              if (count>0)
              {
                 return false;
              }
              else
              {
                switch(table)
                {
                  case "fundstat":
                                def rlta = parseHTML_a();
                                System.err.println("正在插入fund数据..............")
                                rlta.each{p ->
                                    insert_a(p);
                                }
                                System.err.println("fund数据插入完成..............")
                    break;
                  case "boyistat":
                                def rltb = parseHTML_b();
                                System.err.println("正在插入数据..............")
                                rltb.each{p ->
                                    insert_b(p);
                                }
                                System.err.println("boyi数据插入完成..............")
                    break;
                  case "zcggstat":
                                def rltc = parseHTML_c();
                                System.err.println("正在插入数据..............")
                                rltc.each{p ->
                                    insert_c(p);
                                }
                                System.err.println("zcgg数据插入完成..............")
                    break;
                }


                return true; 
              }
          }
          else
          {
            return false;
          }
        }
		private void initConn()
		{

			def driver = "com.mysql.jdbc.Driver";
			def db = "jdbc:mysql://localhost/stockstat?useUnicode=true&characterEncoding=gb2312";
			def user = "root";
			def pwd = "111111";
			sql = Sql.newInstance(db,user,pwd,driver);

		}
		def insert_a(def p)
		{

			p = (HexunFund)p;

	        def sdf=new java.text.SimpleDateFormat("yyyy-MM-dd");
			sql.executeInsert("insert into fundstat(rank,code,name,fund,percent,atype,period,statdate)" +
					"values(?,?,?,?,?,?,?,?)",[p.rank,p.code,p.name,p.fund,p.percent,p.atype,p.period,sdf.format(p.statdate)])

		}
		def insert_b(def p)
		{

			p = (HexunBoyi)p;
	        def sdf=new java.text.SimpleDateFormat("yyyy-MM-dd");
			sql.executeInsert("insert into boyistat(rank,code,name,price,range,atype,period,statdate,big,small,scale)" +
					"values(?,?,?,?,?,?,?,?,?,?,?)",[p.rank,p.code,p.name,p.price,p.range,p.atype,p.period,sdf.format(p.statdate),p.big,p.small,p.scale])

		}
		def insert_c(def p)
		{

			p = (HexunZcgg)p;

	        def sdf=new java.text.SimpleDateFormat("yyyy-MM-dd");
			sql.executeInsert("insert into zcggstat(code,name,days,fund,qujianbi,liutongbi,swap,range,time,statdate,atype)" +
					"values(?,?,?,?,?,?,?,?,?,?,?)",[p.code,p.name,p.days,p.fund,p.qujianbi,p.liutongbi,p.swap,p.range,p.time,sdf.format(p.statdate),p.atype])

		}
		def parseHTML_a()
		{
			ArrayList rlt = new ArrayList<HexunFund>();
			def periods = ["day","week","month","season"];//param
			def atypes = ["zijinjingliurupaiming",
			              "zijinjingliuruzhanchengjiaobilipaiming",
			              "zijinjingliuchupaiming",
			              "zijinjingliuchuzhanchengjiaobilipaiming",
			              "jigouzengcanggegupaiming",
			              "jigoujiancanggegupaiming",
			              "sanhuzengcanggegupaiming",
			              "sanhujiancanggegupaiming"
			              ];

			atypes.eachWithIndex { type,typei->
				periods.eachWithIndex{param,parami ->
					[1,2,3].eachWithIndex {page,pagei ->
						def url = "http://vol.stock.hexun.com/afflux.aspx?type=${typei}&param=${parami}&page=${page}";
						System.err.println("正在处理"+url+"..............")
						//def text = url.toURL().text;
                        URLConnection conn = new URL(url).openConnection();
                        def text = new InputStreamReader(conn.getInputStream(), "gb2312").text
						def startText = "<span class=\"div_x\"></span></div></div></td></tr>";
						text.eachLine(){ line ->
							line = line.toString();
							def start = line.indexOf(startText)
							if(start != -1)
							{
								def dataText = line.substring(start + startText.length());
								def dataAry = dataText.replaceAll("</tr>","").replaceAll("</td>","").split("<tr>");
								dataAry.each{ tr ->
									tr = tr.replaceAll(/(<div).*(<\/div>)$/,"")
									def tds = tr.split("<td");
									def p = new HexunFund();
									def tdi = 0;
									tds.each{td ->
										if(td.trim()!="")
										{
											td = "<td"+td;
											td = td.replaceAll(/^(<td.*?>)/,"");
											td = td.replaceAll(/^(<.*?>)/,"");
											td = td.replaceAll(/(<.*?>)/,"");
											td = td.replaceAll("&nbsp;","");
											td = td.trim();
											if(td+"aa" !="aa")
											{
												switch(tdi)
												{
													case 0:
														p.rank = Integer.parseInt(td);
														break;
													case 1:
														p.code = td;
														break;
													case 2:
														p.name = td;
														break;
													case 3:
														if(typei == 0 || typei == 2 )
														{
															p.fund = Double.parseDouble(td)*1000;
														}
														else
														{
															p.percent = Double.parseDouble(td);
														}
														break;
													case 4:
														if(typei == 0 || typei == 2 )
														{
															p.percent = Double.parseDouble(td);
														}
														break;
												}
												p.atype = type;
												p.period = new String(periods[parami].getBytes(),"gb2312");

												p.statdate = (new Date());
												tdi++;
											}
										}
									}
									if(p.rank!=0)
									{
										rlt.add p;
									}
								}
							}
						}
					};
				};
			}
			return rlt;
		}
		def parseHTML_b()
		{
			ArrayList rlt = new ArrayList<HexunBoyi>();
			def periods = ["day","week","month","season"];
			def atypes = ["jigoujingliuru","sanhujingliuru"];
			atypes.eachWithIndex { type,typei->
			typei += 8;
				periods.eachWithIndex{param,parami ->
						def url = "http://vol.stock.hexun.com/affluxBargain.aspx?type=${typei}&param=${parami}";
						System.err.println("正在处理"+url+"..............")
						//def text = url.toURL().text.toString();
                        URLConnection conn = new URL(url).openConnection();
                        def text = new InputStreamReader(conn.getInputStream(), "gb2312").text
						def startText = "<td height=\"23\" align=\"center\" bgcolor=\"#f2f7f9\" class=\"leftOne\">";
						text.eachLine(){ line ->
							line = line.toString();
							def start = line.indexOf(startText)
							if(start != -1)
							{
								def dataText = line.trim();
								def dataAry = dataText.replaceAll("</tr>","").replaceAll("</td>","").split("<tr>");
								dataAry.each{ tr ->
									def tds = tr.split("<td");
									def p = new HexunBoyi();
									def tdi = 0;
									tds.each{td ->
										if(td.trim()!="")
										{
											td = "<td"+td;
											td = td.replaceAll(/^(<.*?>)/,"");
											td = td.replaceAll(/^(<.*?>)/,"");
											td = td.replaceAll(/(<.*?>)/,"");
											td = td.replaceAll("&nbsp;","");
											td = td.replaceAll("%","");
											td = td.trim();
											if(td+"aa" !="aa")
											{
												//println td
												switch(tdi)
												{
													case 0:
														p.rank = Integer.parseInt(td);
														break;
													case 1:
														p.code = td;
														break;
													case 2:
														p.name = td;
														break;
													case 3:
														p.price = Double.parseDouble(td);
														break;
													case 4:
														p.range = Double.parseDouble(td);
													case 5:
														p.big = Double.parseDouble(td)*1000;
														break;
													case 6:
														p.small = Double.parseDouble(td)*1000;
														break;
													case 7:
														p.scale = Double.parseDouble(td);
														break;
												}
												p.atype = type;
												p.period = new String(periods[parami].getBytes(),"gb2312");
												p.statdate = (new Date());
												tdi++;
											}
										}
									}
									if(p.rank!=0)
									{
										rlt.add p;
									}
								}
							}
						}
				};
			}
			return rlt;
		}
		def parseHTML_c()
		{
			ArrayList rlt = new ArrayList<HexunZcgg>();
			def atypes = ["lianxujigouzengcanggegu",
			              "lianxijigoujiancanggegu"
			              ];
			atypes.eachWithIndex { type,typei->
					[1,2,3].eachWithIndex {page,pagei ->
						def url = "http://vol.stock.hexun.com/zcgg.aspx?type=${typei}&page=${page}";
						System.err.println("正在处理"+url+"..............")
						//def text = url.toURL().text;
                        URLConnection conn = new URL(url).openConnection();
                        def text = new InputStreamReader(conn.getInputStream(), "gb2312").text
						def startText = "<td align=\"center\" height=\"23\" bgcolor=\"#f2f7f9\"  class=\"leftOne\" >";
						text.eachLine(){ line ->
							line = line.toString().trim();
							def start = line.indexOf(startText)
							if(start != -1)
							{
								def dataText = line.substring(start + startText.length());
								def dataAry = dataText.replaceAll("</tr>","").replaceAll("</td>","").split("<tr>");
								dataAry.each{ tr ->
									def tds = tr.split("<td");
									def p = new HexunZcgg();
									def tdi = 0;
									tds.each{td ->
										if(td.trim()!="")
										{
											td = "<td"+td;
											td = td.replaceAll(/^(<td.*?>)/,"");
											td = td.replaceAll(/^(<.*?>)/,"");
											td = td.replaceAll(/(<.*?>)/,"");
											td = td.replaceAll("&nbsp;","");
											td = td.trim();
											if(td+"aa" !="aa")
											{

												switch(tdi)
												{
													case 0:
														p.code = td;
														break;
													case 1:
														p.name = td;
														break;
													case 2:
														p.days = Integer.parseInt(td);
														break;
													case 3:
														p.fund = Double.parseDouble(td)*1000;
														break;
													case 4:
														p.qujianbi = Double.parseDouble(td);
														break;
													case 5:
														p.liutongbi = Double.parseDouble(td);
														break;
													case 6:
														p.swap = Double.parseDouble(td);
														break;
													case 7:
														p.range = Double.parseDouble(td);
														break;
													case 8:
														p.time = td;
														break;
												}
												p.atype = type;
												p.statdate = (new Date());
												tdi++;
											}
										}
									}
									if(p.days!=0)
									{
										rlt.add p;
										//println p;
									}
								}
							}
						}
					};
			}
			return rlt;
		}
}
